<?php

/**
 * @access public
 * @author Kevin
 * @package Mapping_Dead.Modele
 */
class PDO_Connecteur {

    private static $instance;
    protected $connexion;
    protected $server;
    protected $name;
    protected $user;
    protected $password;
    protected $config;

    private function __construct() {
        require_once("/../lib/dbconfig.php");
        global $db_config;

        $this->server = 'localhost';
        $this->name = 'mappingdead';
        $this->user = 'root';
        $this->password = '';
        $this->config = $db_config;
        $this->connexion = new PDO("mysql:host=$this->server;dbname=$this->name", $this->user, $this->password);
        $this->connexion->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }

    public static function getInstance() {
        if (self::$instance == null) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    private function __clone() {
        
    }

    /**
     * @access public
     * @param String login
     * @param String mdr
     * @return boolean
     * @ParamType login String
     * @ParamType mdr String
     * @ReturnType boolean
     */
    public function seConnecter($login, $mdr) {
        // Not yet implemented
    }

    /**
     * @access public
     * @return boolean
     * @ReturnType boolean
     */
    public function seDeconnecter() {
        // Not yet implemented
    }

    /**
     * @access public
     * @param String login
     * @return boolean
     * @ParamType login String
     * @ReturnType boolean
     */
    public function verifLogin($login) {
        // Not yet implemented
    }

    /**
     * @access public
     * @param String mdp
     * @return boolean
     * @ParamType mdp String
     * @ReturnType boolean
     */
    public function verifMdp($mdp) {
        // Not yet implemented
    }

    /**
     * @access public
     * @param String table
     * @param String[] infos
     * @return String[]
     * @ParamType table String
     * @ParamType infos String[]
     * @ReturnType String[]
     */
    public function getInformations($table, $args = null) {
        $firstKey = true;
        $res = array();
        $request = "SELECT * FROM $table";
        if ($args != null && is_array($args) && !empty($args)) {
            $request .= " WHERE ";
            foreach ($args as $key => $value) {
                if (!$firstKey) {
                    $request .= " AND ";
                }
                $request .= $key . "=" . $this->connexion->quote($value);
                $firstKey = false;
            }
            $request .= ";";
        }
        $query = $this->connexion->query($request);
        if (!$query) {
            return false;
        }

        while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
            $res[] = $row;
        }
        return $res;
    }

    /**
     * @access public
     * @param String table
     * @param String[] infos
     * @return void
     * @ParamType table String
     * @ParamType infos String[]
     * @ReturnType void
     */
    public function insertInformations($table, $args = null) {
        $listParam = null;
        $premierParam = true;

        if ($args == null) {
            $listParam = $this->config[$table];
        } else if (!is_array($args) || empty($args)) {
            return false;
        } else {
            $listParam = $args;
        }

        $request = "INSERT INTO $table (";
        $listeValues = '';
        foreach ($listParam as $column => $value) {
            if (!$premierParam) {
                $request .=', ';
                $listeValues .=', ';
            }
            $request .= $column;
            $listeValues .= $this->connexion->quote($value);
            $premierParam = false;
        }
        $request .= ") VALUES (" . $listeValues . ");";
        $res = $this->connexion->query($request);

        if (!$res) {
            return false;
        }
        return true;
    }

    /**
     * @access public
     * @param String table
     * @param String[] infos
     * @param String[] clause
     * @return void
     * @ParamType table String
     * @ParamType infos String[]
     * @ParamType clause String[]
     * @ReturnType void
     */
    public function updateInformations($table, array $args, array $filters) {
        $listParam = null;
        $firstParam = true;

        if ($args == null) {
            $listParam = $this->config[$table];
        } else if (!is_array($args) || empty($args)) {
            return false;
        } else {
            $listParam = $args;
        }

        $request = "UPDATE $table SET ";
        if ($args != null && is_array($args) && !empty($args)) {
            foreach ($args as $key => $value) {
                if (!$firstParam) {
                    $request .= ", ";
                }
                $request .= $key . "=" . $this->connexion->quote($value);
                $firstParam = false;
            }
            $firstParam = true;
        }

        if ($filters != null && is_array($filters) && !empty($filters)) {
            $request .= " WHERE ";
            foreach ($filters as $key => $value) {
                if (!$firstParam) {
                    $request .= " AND ";
                }
                $request .= $key . "=" . $this->connexion->quote($value);
                $firstParam = false;
            }
            $request .= ";";
        }

        $res = $this->connexion->query($request);
        if (!$res) {
            return false;
        }
        return true;
    }

    /**
     * @access public
     * @param String table
     * @param String[] clause
     * @ParamType table String
     * @ParamType clause String[]
     */
    public function deleteInformations($table, array $filters) {
        if ($filters == null || !is_array($filters) || empty($filters)) {
            return false;
        }

        $firstParam = true;
        $request = "DELETE FROM $table WHERE ";
        foreach ($filters as $key => $value) {
            if (!$firstParam) {
                $request .= " AND ";
            }
            $request .= $key . "=" . $this->connexion->quote($value);
            $firstParam = false;
        }
        $request .= ";";
//        echo 'Cette méthode est désactivée';
//        var_dump($request);
//        exit();
        $res = $this->connexion->query($request);
        if (!$res) {
            return false;
        }
        return true;
    }

    public function execute($request) {
        $res = array();
        if ($this->connexion != null) {
            return false;
        }
        $query = $this->connexion->query($request);

        return $res;
    }

    public function getNbLine($tableName, $args) {
        $request = "SELECT count(*) from $tableName";
        $firstKey = true;
        if ($args != null && is_array($args) && !empty($args)) {
            $request .= " WHERE ";
            foreach ($args as $key => $value) {
                if (!$firstKey) {
                    $request .= " AND ";
                }
                $request .= $key . "=" . $this->connexion->quote($value);
                $firstKey = false;
            }
            $request .= ";";
        }
        $query = $this->connexion->query($request);
        $res = $query->fetch(PDO::FETCH_NUM);
        return $res[0];
    }

}

?>